package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 加入购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查寻当前用户是否将该商品添加到购物车中 => 查询sql
        //此时的shoppingCart 3种情况
        // 1.dishId + userId     =>  结果最多是1条
        // 2.dishId+口味 + userId =>  结果最多是1条
        // 3.setmealId + userId  =>  结果最多是1条
        List<ShoppingCart> list = shoppingCartMapper.findByOptions(shoppingCart);
        if (list != null && list.size() == 1) {
            //查到数据,只需要修改个数
            ShoppingCart sc = list.get(0);
            //1.修改个数
            sc.setNumber(sc.getNumber() + 1);
            shoppingCartMapper.updateNumberById(sc);
        } else {
            //没查到记录,第一次添加购物车
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //2. 菜品 => 查询菜品
                Dish dish = dishMapper.findById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //3. 套餐 => 查询套餐
                Setmeal setmeal = setmealMapper.findById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //存入数据库
            shoppingCartMapper.save(shoppingCart);
        }

    }

    /**
     * 根据用户id查询购物车
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> findShoppingCartsByUserId(Long userId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.findByOptions(shoppingCart);
        return list;
    }

    /**
     * 根据用户id清空购物车
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(Long userId) {
        shoppingCartMapper.deleteByUserId(userId);
    }
}
